//
//  YCLStatusCell.swift
//  新浪微博01
//
//  Created by yaochunlin on 16/4/14.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit
import SnapKit

class YCLStatusCell: UITableViewCell {
    
    //记录之前bottomView的top约束
    var bottomViewTopCons: Constraint?
    
    
    
    //微博模型
    var status:YCLStatus? {
        
        didSet{
            
            statusOriginalView.status = status
            
            //卸载之前的约束
            bottomViewTopCons?.uninstall()
            
            //如果有转发的微博
            if let retweetStatus = status?.retweeted_status {
                
                statusRetweetView.hidden = false
                statusRetweetView.status = retweetStatus
                
                //重新设置约束
                statusBottomView.snp_makeConstraints(closure: { (make) in
                    
                    bottomViewTopCons = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }else{
                
                statusRetweetView.hidden = true
                statusBottomView.snp_makeConstraints(closure: { (make) in
                    
                    bottomViewTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
                
            }
            
        }
        
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        // 设置cell背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        //设置ui
        prepareUI()
        
    }
    
    private func prepareUI() {
        
        //添加子控件
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statusBottomView)

        
        //设置原始view约束
        statusOriginalView.snp_makeConstraints { (make) in
            make.leading.trailing.equalTo(contentView)
            //顶部有间距
            make.top.equalTo(contentView).offset(10)
        }
        
        //设置转发view的约束
        statusRetweetView.snp_makeConstraints { (make) in
            
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.trailing.leading.equalTo(contentView)
        }
        
        
        //设置底部view约束
        statusBottomView.snp_makeConstraints { (make) in
            
            make.leading.trailing.equalTo(contentView)
            bottomViewTopCons = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(44)
        }
        
        //系统会让cell取contentview得高度
        contentView.snp_makeConstraints { (make) in
            
            make.leading.trailing.top.equalTo(self)
            
            //底部由内容决定（参照 statusBottomView）
            make.bottom.equalTo(statusBottomView)
        }
        
    }
    
    
    //MARK 懒加载控件
    
    //原创view
    private lazy var statusOriginalView: YCLStatusOriginalView = YCLStatusOriginalView()
    
    //底部view
    private lazy var statusBottomView:YCLStatusBottomView = YCLStatusBottomView()
    
    //被转发的view
    private lazy var statusRetweetView:YCLStatusRetweetView = YCLStatusRetweetView()
    
}
